function ajaxPost(url, data, responseSuccess){
    $.ajax({
        type: "POST",
        url: url,
        data:  data,
        async: true,
        beforeSend: function () {
            jz = layer.load(0, {shade: false}); //0代表加载的风格，支持0-2
        },
        error: function (request) {
            layer.close(jz);
            layer.alert('网络错误',{icon:2}, function(index){
                layer.close(index);
            });
        },
        success: function (data) {
            //关闭加载层
            layer.close(jz);
            if (data.code == 1) {
                if(typeof responseSuccess != undefined){
                    responseSuccess(data);
                }
            }else if(data.code == -1){
                layer.msg(data.msg, {
                    icon: 2,
                    time: 5000
                }, function(){
                    window.location.href = '/index/index.html';
                });
            } else {
                layer.msg(data.msg, {
                    icon: 2,
                    time: 2000
                });
            }
        }
    });
}

function ajaxPostCommon(formId, responseSuccess){
    var url = $(formId).attr('action');
    var data = $(formId).serialize();
    $.ajax({
        type: "POST",
        url: url,
        data:  data,
        async: true,
        beforeSend: function () {
            jz = layer.load(0, {shade: false}); //0代表加载的风格，支持0-2
        },
        error: function (request) {
            layer.close(jz);
            layer.alert('网络错误',{icon:2}, function(index){
                layer.close(index);
            });
        },
        success: function (data) {
            //关闭加载层
            layer.close(jz);
            if (data.code == 1) {
                layer.msg(data.msg, {
                    icon: 1,
                    time: 2000
                });
                if(typeof responseSuccess != undefined){
                    responseSuccess(data);
                }
            }else if(data.code == -1){
                layer.msg(data.msg, {
                    icon: 2,
                    time: 5000
                }, function(){
                    window.location.href = '/index/index.html';
                });
            } else {
                layer.msg(data.msg, {
                    icon: 2,
                    time: 2000
                });
            }
        }
    });
}

// $('body').on('click','.toupiao',function () {
//     layer2.open({
//         type: 1,
//         shadeClose: true,
//         content:'<div id="sucpop2"><img src="/uploads/vote/151672257273897.jpg"><div class="lapiao"></div></div>',
//         btn:'<i class="iconfont icon-guanbi closebtn3"></i>',
//     });
// });
    $('body').on('click','.toupiao',function () {
        var applyId = $(this).data('id');
        ajaxPost('/apply/vote', {'apply_id': applyId}, function(data) {
            switch (data.data.code){
                case 2: // 余额不足
                    layer.msg(data.data.msg);
                    break;
                case 3: // 已获得过该红包
                    layer.msg(data.data.msg);
                    break;
                case 4: // 获取红包
                    window.apply = data.data.apply;
                    layer2.open({
                        type: 1,
                        shadeClose: true,
                        content:'<div id="sucpop2"><img src="'+ data.data.jpg +'"><div class="lapiao"></div></div>',
                        btn:'<i class="iconfont icon-guanbi closebtn3"></i>',
                    });
                    break;
                default:
                    layer.msg(data.msg);
            }
        });
    });

    $('body').on('click','.layclose',function(){
        layer2.closeAll();
        $('.cover').fadeIn();
    })

    $('.coverbtn').on('click',function () {
        $('.cover').fadeOut();
    })

    // $('.lapiao').on('click',function () {
    //     $('.cover').fadeIn();
    // })


        
        
        
    $('.tabbar>a').on('click',function () {
            $(this).addClass('active');
            $(this).siblings().removeClass('active');
            var i = $(this).index();
            $('.tabcontent .toulist').eq(i).addClass('active');
            $('.tabcontent .toulist').eq(i).siblings().removeClass('active');
    })

        $('#pay').on('click',function () {
            var html = '<div class="popmodal" id="paypop">' +
                '<div class="redbox">' +
                '<div class="pay">' +
                '<button class="chongbtn">充值感谢金</button>' +
                '<p class="gray">我的余额：<span>'+ balance +'</span>元</p>' +
                '<p class="gray">充值部分的余额不能用于提现</p>' +
                '<div class="chonggroup">' +
                '<button class="active chong" data-money="5"><span>5</span>元</button>' +
                '<button class="chong" data-money="10"><span>10</span>元</button>' +
                '<button class="chong" data-money="20"><span>20</span>元</button>' +
                '<button class="chong" data-money="30"><span>30</span>元</button>' +
                '<button class="chong" data-money="50"><span>50</span>元</button>' +
                '<button class="chong" data-money="100"><span>100</span>元</button>' +
                '</div>' +
                '<div class="chongroup">' +
                '<button type="reset" class="rebtn">取消</button>' +
                '<a href="javascript:void(0);" class="subtn notarize-recharge">确认充值</a>' +
                '</div></div></div></div>';
            layer2.open({
                type: 1,
                shadeClose: true,
                content:html,
            });
        });

    // 充值选择金额
    $('body').on('click','.chong',function(){
        $(this).addClass('active');
        $(this).siblings().removeClass('active');
    })

    $('body').on('click','.rebtn',function(){
        layer2.closeAll();
    })
    $('body').on('click','.chongz',function(){
        ajaxPostCommon('#set-brokerage', function (data) {
            brokerage = data.data.result;
            layer2.closeAll();
        })
    });

    $('#edit').on('click',function () {
        var html = '<div class="popmodal" id="paypop"><div class="bluebox"><div class="pay"><button class="chongbtn">充值红包</button><p>充值后，好友帮你拉票/分享，且产生投票后，您即对该分享人和投票人进行红包奖励；您本人不参与自己发起的红包奖励！</p>' +
            '<form action="/user/setBrokerage" id="set-brokerage">' +
            '<div class="inputgroup">' +
            '<label>拥金分成</label>';
            if(brokerage){
                html += '<input type="text" name="share_ratio" value="'+ brokerage.share_ratio +'" placeholder="分享人"> <span>:</span> <input type="text" name="vote_ratio" value="'+ brokerage.vote_ratio +'" placeholder="投票人">';
            } else {
                html += '<input type="text" name="share_ratio" placeholder="分享人"> <span>:</span> <input type="text" name="vote_ratio" placeholder="投票人">';
            }
            html += '<p class="gray">设置分享人佣金分成比例，输入0-10数字</p></div>' +
            '<div class="inputgroup"><label>佣金发放</label>';
            if(brokerage) {
                html += '<input type="number" name="max_money" value="'+ brokerage.max_money +'" placeholder="最大金额"> <span>-</span> <input type="number" name="min_money" value="'+ brokerage.min_money +'" placeholder="最小金额">';
            } else {
                html += '<input type="number" name="max_money" placeholder="最大金额"> <span>-</span> <input type="number" name="min_money" placeholder="最小金额">';
            }
            html += '<p class="gray">随机去最小金额和最大金额中间数，保留两位小数，每人</p><p class="c-red">实际收到的佣金=随机佣金*分成</p>' +
            '<p class="c-green">活动结束后的第三天可申请退款。</p>       ' +
            '</div><div class="inputgroup">' +
            '<label class="weui-label">红包充值</label><span class="c-red">'+ balance +'</span> 元</div></div>' +
            '<div class="chongroup"><button type="reset" class="rebtn">取消</button><a href="javascript:void(0);" class="subtn chongz">确认充值</a></div>' +
            '</form></div></div></div>';
        layer2.open({
            type: 1,
            shadeClose: true,
            content: html,
        });
    });

    $(function() {
        FastClick.attach(document.body);
    });

    // 拉票成功回调函数
    function shareSuccess() {
        ajaxPost('/index/shareSuccess', {apply_id: apply.id}, function(data) {
            switch (data.data.code){
                case 1: // 本人分享
                    layer.msg(data.data.msg);
                    break;
                case 2: // 余额不足
                    layer.msg(data.data.msg);
                    break;
                case 3: // 已获得过该红包
                    layer.msg(data.data.msg);
                    break;
                case 4: // 获取红包
                    layer.msg(data.data.msg);
                    break;
                default:
                    layer.msg(data.msg);
            }
        });
    }

    // 确认充值
    var payParams = {};
    $('body').on('click', '.notarize-recharge', function() {
        layer.confirm('亲，支付会收取一定1%的手续费，确定继续支付吗', {}, function () {
            var amount = $('#paypop .chong.active').data('money');
            console.log($('#paypop .chong.active'), amount);
            var data = {amount: amount};
            $.post('/Deal/recharge', data, function (data) {
                if(typeof data == 'string') eval("var data = "+data+';');
                payParams = data;
                // alert(JSON.stringify(payParams));
                onBridgeReady();
            });
        })
    })
    function onBridgeReady() {
        WeixinJSBridge.invoke(
            'getBrandWCPayRequest', payParams,
            function (res) {
                // 使用以上方式判断前端返回,微信团队郑重提示:res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。
                if (res.err_msg == "get_brand_wcpay_request:ok") {
                    layer.msg('充值成功');

                }
            }
        );
    }
